package com.mcxx.modules.tool.service.impl;

import com.mcxx.common.ExternalFileUtil;
import com.mcxx.modules.external.yinhai.entity.*;
import com.mcxx.modules.external.yinhai.service.read.*;
import com.mcxx.modules.external.yinhai.service.write.*;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Service
@TaTransactional
public class YinhaiSubsistenceService {

    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(YinhaiSubsistenceService.class);

    private ExecutorService executorService = Executors.newFixedThreadPool(50);

    @Autowired
    private YHBAX99ReadService yhBAX99ReadService;
    @Autowired
    private YHBAP96ReadService yhBAP96ReadService;
    @Autowired
    private YHBAF01ReadService yhBAF01ReadService;
    @Autowired
    private YHBAP01ReadService yhBAP01ReadService;
    @Autowired
    private YHBAF97ReadService yhBAF97ReadService;
    @Autowired
    private YHBAP98ReadService yhBAP98ReadService;
    @Autowired
    private YHBAP97ReadService yhBAP97ReadService;

    @Autowired
    private YHBAX99WriteService yhBAX99WriteService;
    @Autowired
    private YHBAP96WriteService yhBAP96WriteService;
    @Autowired
    private YHBAF01WriteService yhBAF01WriteService;
    @Autowired
    private YHBAP01WriteService yhBAP01WriteService;
    @Autowired
    private YHBAF97WriteService yhBAF97WriteService;
    @Autowired
    private YHBAP97WriteService yhBAP97WriteService;
    @Autowired
    private YHBAP98WriteService yhBAP98WriteService;

    private static String donePath = "/home/society/application/yinhai/yh_done.txt";
    private static String errPath = "/home/society/application/yinhai/yh_error.txt";


    public String dibaotekunData(String strAreaCodes, String strMonths, String busiCode){


        logger.info("读取地区：{}", strAreaCodes);
        List<YHBAX99Entity> fundPayListByYinhai = yhBAX99ReadService.getFundPayListByYinhai("01", strAreaCodes, strMonths, busiCode);
        logger.info("查询到{}条数据", fundPayListByYinhai.size());
        if(CollectionUtils.isNotEmpty(fundPayListByYinhai)){
            int no = 0;
            for (YHBAX99Entity d : fundPayListByYinhai){
                try{
                    String orderId = d.getABAP9601();
                    if(ExternalFileUtil.traverseFolder(donePath, orderId)){
                        logger.info("oId 已处理");
                    }else{
                        logger.info("开始执行数量," + orderId + ","+ no+"/"+fundPayListByYinhai.size());

                        executorService.execute(new executeDibaoData(d,orderId,d.getABAF0101()));
                    }
                }catch (Exception ex){
                    logger.error(ex.getMessage(),ex);
                }
                no++;
            }
        }
        return null;
    }

    class executeDibaoData implements Runnable{
        private String orderId;
        private String familyId;
        private YHBAX99Entity d;


        public executeDibaoData(YHBAX99Entity d,String orderId,String familyId) {
            this.d = d;
            this.orderId = orderId;
            this.familyId = familyId;
        }

        @Override
        public void run() {
            try{

                YHBAP96Entity orderDetailByYinhai = yhBAP96ReadService.getOrderDetailByYinhai(orderId,familyId);
                if(null == orderDetailByYinhai){
                    ExternalFileUtil.writeLog(errPath,"未查询到业务记录,orderId:"+orderId);
                    return;
                }

                YHBAF01Entity familyMainByYinhai = yhBAF01ReadService.getFamilyMainByYinhai(orderId, familyId);
                if(null == familyMainByYinhai){
                    ExternalFileUtil.writeLog(errPath,"未查询到家庭记录,orderId:"+orderId);
                    return;
                }

                List<YHBAP01Entity> memberByYinhai = yhBAP01ReadService.getMemberByYinhai(orderId, familyId);
                if(CollectionUtils.isEmpty(memberByYinhai)){
                    ExternalFileUtil.writeLog(errPath,"未查询到成员记录,orderId:"+orderId);
                    return;
                }

                YHBAF97Entity familySalaryByYinhai = yhBAF97ReadService.getFamilySalaryByYinhai(orderId, familyId);
                if(null == familySalaryByYinhai){
                    ExternalFileUtil.writeLog(errPath,"未查询到家庭待遇记录,orderId:"+orderId);
                    return;
                }

                List<YHBAP97Entity> memberSalaryByYinhai = yhBAP97ReadService.getMemberSalaryByYinhai(orderId, familyId);
                if(CollectionUtils.isEmpty(memberSalaryByYinhai)){
                    ExternalFileUtil.writeLog(errPath,"未查询到成员待遇记录,orderId:"+orderId);
                    return;
                }

                List<YHBAP98Entity> memberChangeByYinhai = yhBAP98ReadService.getMemberChangeByYinhai(orderId, familyId);
                if(CollectionUtils.isEmpty(memberChangeByYinhai)){
                    ExternalFileUtil.writeLog(errPath,"未查询到成员待遇变化记录,orderId:"+orderId);
                    return;
                }



                this.savaData(orderDetailByYinhai,familyMainByYinhai,memberByYinhai,familySalaryByYinhai,memberSalaryByYinhai,memberChangeByYinhai);
            }catch (Exception ex){
                logger.error(ex.getMessage(),ex);
                ExternalFileUtil.writeLog(errPath,ex.getMessage());
            }
        }

        public void savaData(YHBAP96Entity p96,YHBAF01Entity f01,List<YHBAP01Entity> p01List,
                            YHBAF97Entity f97,List<YHBAP97Entity> p97List,List<YHBAP98Entity> p98List) throws Exception {
            int i = yhBAX99WriteService.insertIgnoreNull(d);
            if(i <= 0){
                ExternalFileUtil.writeLog(errPath,"保存资金发放信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }
            int i1 = yhBAP96WriteService.insertIgnoreNull(p96);
            if(i1 <= 0){
                ExternalFileUtil.writeLog(errPath,"保存救助审批信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }
            int i2 = yhBAF01WriteService.insertIgnoreNull(f01);
            if(i2 <= 0){
                ExternalFileUtil.writeLog(errPath,"保存家庭信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }
            int i3 = yhBAF97WriteService.insertIgnoreNull(f97);
            if(i3 <= 0){
                ExternalFileUtil.writeLog(errPath,"保存家庭待遇信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }
            int inserts = yhBAP01WriteService.inserts(p01List);
            if(inserts <= 0){
                ExternalFileUtil.writeLog(errPath,"保存成员信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }
            int inserts1 = yhBAP98WriteService.inserts(p98List);
            if(inserts1 <= 0){
                ExternalFileUtil.writeLog(errPath,"保存成员待遇变化信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }
            int inserts2 = yhBAP97WriteService.inserts(p97List);
            if(inserts2 <= 0){
                ExternalFileUtil.writeLog(errPath,"保存成员待遇信息失败,orderId:"+orderId);
                throw new RuntimeException();
            }

            ExternalFileUtil.writeLog(donePath,orderId);
        }


    }

}
